package com.liu.common.exception;

import com.liu.common.bean.ResultBean;
import com.liu.common.controller.BaseController;
import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.apache.tomcat.util.http.fileupload.impl.SizeLimitExceededException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.xml.crypto.dsig.keyinfo.RetrievalMethod;

@RestControllerAdvice
public class GlobalExceptionHandler extends BaseController {


    @Value("${spring.servlet.multipart.max-file-size}")
    long maxFileSize;

    @ExceptionHandler(Exception.class)
    public ResultBean handlerSizeLimitExceededException(Exception e){
        e.printStackTrace();
        return  fail("接口请求异常");
    }

    @ExceptionHandler(ValidateException.class)
    public ResultBean handlerValidateException(ValidateException e){
        return fail("验证错误",e.getError());
    }

    @ExceptionHandler(FileSizeLimiException.class)
    public ResultBean handlerFileSizeLimiException(FileSizeLimiException e){
        return  fail(e.getMessage());
    }

    @ExceptionHandler(FileNotExistException.class)
    public ResultBean handlerFileNotExistException(FileNotExistException e){
        return  fail(e.getMessage());
    }

    @ExceptionHandler({MaxUploadSizeExceededException.class,SizeLimitExceededException.class,FileSizeLimitExceededException.class})
    public ResultBean handlerFileNotExistException(Exception e){
        return  fail("上传文件超过指定大小");
    }

    /**
     * 统一处理业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public ResultBean handlerBusinessException(BusinessException e){
        e.printStackTrace();
        return  fail(e.getMessage());
    }



}
